import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:lcapp/api/interface/zixun_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/data/zixun_data_entity.dart';
import 'package:lcapp/model/refresh/refresh.dart';
import 'package:lcapp/page/min/min_zixun_item.dart';

class SearchZXPage extends StatefulWidget {
  final String searchValue;
  const SearchZXPage({Key? key, required this.searchValue}) : super(key: key);

  @override
  _SearchZXPageState createState() => _SearchZXPageState();
}

class _SearchZXPageState extends State<SearchZXPage> {
  List<ZixunDataEntity> zixuns = [];
  int pageNum = 1;
  int pageSize = 10;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getZixun();
  }

  getZixun()async{

    ResponseData res = await ZiXunAPI.getZiXunList(pageNum, pageSize,keyword: widget.searchValue);
    if(res.code==200){
      if(pageNum==1){
        zixuns.clear();
      }
      List arr = res.data??[];
      for (var e in arr) {
        ZixunDataEntity entity = ZixunDataEntity().fromJson(e);
        zixuns.add(entity);
      }

    }
    if(mounted){setState(() {
    });}
  }


  @override
  void didUpdateWidget(covariant SearchZXPage oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    if(oldWidget.searchValue != widget.searchValue){
      pageNum = 1;
      getZixun();
    }
  }

  @override
  Widget build(BuildContext context) {
    return EasyRefresh(
      footer: myRefreshFooter(),
      header: myRefreshHeader(),
      onRefresh: ()async{
        pageNum=1;
        getZixun();
      },
      onLoad: ()async{
        pageNum++;
        getZixun();
      },
      child: ListView.builder(
          padding: EdgeInsets.zero,
          shrinkWrap: true,
          itemCount: zixuns.length,
          itemBuilder: (BuildContext context, int i) {
            return MinZixunItem(entity: zixuns[i], clickFun: (){},);
          }),
    );
  }
}
